﻿using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Reflection.Emit;

namespace Api_Core_Spider.Data
{
    /// <summary>
    /// 激活模块的数据库上下文，用于与数据库交互
    /// </summary>
    public class ActivationDbContext : DbContext
    {
        public ActivationDbContext(DbContextOptions<ActivationDbContext> options)
            : base(options)
        { }

        /// <summary>
        /// 激活记录集合
        /// </summary>
        public DbSet<Activation> Activations { get; set; }

        /// <summary>
        /// 产品密钥集合
        /// </summary>
        public DbSet<ProductKey> ProductKeys { get; set; }
        /// <summary>
        /// 配置数据库模型
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置Activation实体
            modelBuilder.Entity<Activation>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.MachineCode).IsRequired().HasMaxLength(100);
                entity.Property(e => e.ProductKey).IsRequired().HasMaxLength(100);
                entity.Property(e => e.ActivationCode).IsRequired().HasMaxLength(100);
                entity.Property(e => e.UserName).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
                entity.Property(e => e.ActivatedDate).IsRequired();
                entity.Property(e => e.ExpireDate).IsRequired();
                entity.Property(e => e.IsActive).IsRequired().HasDefaultValue(true);

                // 添加索引
                entity.HasIndex(e => e.MachineCode);
                entity.HasIndex(e => e.ProductKey);
                entity.HasIndex(e => e.ActivationCode);
            });

            // 配置ProductKey实体
            modelBuilder.Entity<ProductKey>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Key).IsRequired().HasMaxLength(100);
                entity.Property(e => e.ProductId).IsRequired().HasMaxLength(50);
                entity.Property(e => e.UserId).IsRequired().HasMaxLength(50);
                entity.Property(e => e.CreatedDate).IsRequired();
                entity.Property(e => e.ValidDays).IsRequired();
                entity.Property(e => e.IsUsed).IsRequired().HasDefaultValue(false);

                // 添加唯一索引：确保每个密钥只存在一次
                entity.HasIndex(e => e.Key).IsUnique();
            });
        }
    }

    /// <summary>
    /// 激活记录实体，存储激活相关信息
    /// </summary>
    public class Activation
    {
        /// <summary>
        ///   // 主键
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        ///   // 机器码
        /// </summary>
        public string MachineCode { get; set; }
        /// <summary>
        ///   // 产品密钥
        /// </summary>
        public string ProductKey { get; set; }
        /// <summary>
        ///   // 激活码
        /// </summary>
        public string ActivationCode { get; set; }
        /// <summary>
        ///   // 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        ///   // 邮箱
        ///   </summary>
        public string Email { get; set; }
        /// <summary>
        ///   // 激活时间
        /// </summary>
        public DateTime ActivatedDate { get; set; }
        /// <summary>
        ///   // 过期时间
        /// </summary>
        public DateTime ExpireDate { get; set; }
        /// <summary>
        ///   // 是否激活状态
        /// </summary>
        public bool IsActive { get; set; }
    }

    /// <summary>
    /// 产品密钥实体，存储产品密钥相关信息
    /// </summary>
    public class ProductKey
    {
        /// <summary>
        /// 主键
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 产品密钥密钥内容（如 XXXX-XXXX-XXXX-XXXX）
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 产品ID产品ID（区分不同产品）
        /// </summary>
        public string ProductId { get; set; }
        /// <summary>
        /// 用户ID用户ID（区分不同用户）
        /// </summary>
        public string UserId { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedDate { get; set; }
        /// <summary>
        /// 有效天数
        /// </summary>
        public int ValidDays { get; set; }
        /// <summary>
        /// 是否已使用
        /// </summary>
        public bool IsUsed { get; set; }
    }
}
